################################################################################
#### Merging Receivers and Senders (Study 2)
################################################################################

##########
# Set up #
##########

# Clear global environment
rm(list=ls())

# Set working directory 
setwd("/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata")

# Load packages 
library(tidyverse)
library(dplyr)

# Read in Study 2 Receivers data
df_receivers <- read.csv("receivers_2_working_wide.csv")

#########################################
# Convert Data Frame to the Long Format #
#########################################

# Select relevant variables and make sure that they all have the same class so that
# The conversion works 

df2 <- subset(df_receivers, select = c(respid_env, respid_imm,
                                       respid_trans, post_env, post_imm,
                                       post_trans, pre_env, pre_imm, 
                                       pre_trans, argueside_env, argueside_imm,
                                       argueside_trans, issueside_env, issueside_imm,
                                       issueside_trans, quarkelt1_env, quarkelt1_imm, 
                                       quarkelt1_trans, quarkelt2_env, quarkelt2_imm, 
                                       quarkelt2_trans, persuasive_env, persuasive_imm, 
                                       persuasive_trans, clarity_env, clarity_imm, 
                                       clarity_trans, reasoning_env, reasoning_imm,
                                       reasoning_trans, binary_env, binary_imm, 
                                       binary_trans, movement_trans, movement_imm, movement_env,
                                       respondent, partyid_7, anchor, controlcond,
                                       edu, female_main, female_alt, age, income, 
                                       state_residence, race)) %>% 
  mutate_at(vars(respid_env, respid_imm,
                 respid_trans, post_env, post_imm,
                 post_trans, pre_env, pre_imm, 
                 pre_trans,argueside_env, argueside_imm,
                 argueside_trans, issueside_env, issueside_imm,
                 issueside_trans, quarkelt1_env, quarkelt1_imm, 
                 quarkelt1_trans, quarkelt2_env, quarkelt2_imm, 
                 quarkelt2_trans, persuasive_env, persuasive_imm, 
                 persuasive_trans, clarity_env, clarity_imm, 
                 clarity_trans, reasoning_env, reasoning_imm,
                 reasoning_trans, binary_env, binary_imm, 
                 binary_trans, movement_trans, movement_imm, movement_env,
                 respondent, partyid_7, anchor, controlcond,
                 edu, female_main, female_alt, age, income, 
                 state_residence, race), .funs = as.character)




# Make df in the long format
df_long <- df2 %>%
  mutate(across(everything(), as.character)) %>%
  pivot_longer(
    cols = c(respid_env, respid_imm, respid_trans,
             pre_env, pre_imm, pre_trans, 
             post_env, post_imm, post_trans,
             argueside_env, argueside_imm, argueside_trans, 
             issueside_env, issueside_imm, issueside_trans, 
             quarkelt1_env, quarkelt1_imm, quarkelt1_trans, 
             quarkelt2_env, quarkelt2_imm, quarkelt2_trans,
             reasoning_env, reasoning_imm, reasoning_trans,
             persuasive_env, persuasive_imm, persuasive_trans, 
             clarity_env, clarity_imm, clarity_trans, 
             binary_env, binary_imm, binary_trans, 
             movement_env, movement_imm, movement_trans),  # Specify the columns to pivot
    names_to = c('variable', 'issue'), 
    names_sep = "_",  # Specify the separator to split the names
    values_to = 'value'
  ) %>% 
  pivot_wider(names_from = 'variable', values_from = 'value')

# Exclude respid's with no infomration
df_long <- df_long %>% 
  filter(respid != "") 

############
# Export 1 #
############

# Export the long form of the Second Receivers Survey Data

# Specify the folder path
folder_path <- "/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata/"

# Create the full file path including the filename and extension 
file_path <- file.path(folder_path, "receivers_2_working_long.csv")

# Export the dataframe to CSV 
write.csv(df_long, file = file_path, row.names = FALSE)

# Check if file exists 
file.exists(file_path) 

  
################
# Senders Data #
################
df_senders <- read.csv("senders_working2_v3.csv")

# Filter to only include participants in the Senders' survey who chose to write an argument
df_senders <- df_senders %>% 
  filter(write_yes != FALSE)


###############################
# Merge Study 2 recievers and senders #
###############################

# Note that some of the demographic variables have the same name in both 
# datasets. To avoid names like race.x and race.y, we rename the
# variables in the receivers data to race_rec, income_rec etc.

# Check which column names overlap on the sender and receiver dfs
# Get the column names of both dataframes
cols_df_long <- colnames(df_long)
cols_df_senders <- colnames(df_senders)

# Find the common column names
intersect(cols_df_long, cols_df_senders)

# Add the rec suffix to indicate that those columns belong to the S24 receiver
# dataframe 
df_long <- df_long %>% 
  rename(partyid_7_rec = partyid_7, 
         edu_rec = edu, 
         age_rec = age, 
         income_rec = income, 
         race_rec = race)



persuasion_merged <- inner_join(df_long, df_senders, by = "respid")

############
## Export the merged form of Study 2 ##
############ 


# Specify the folder path

folder_path <- "/Users/martinnaunov/Desktop/Desktop - Martin’s MacBook Pro (2)/Persuasion/2_workingdata/"

# Create the full file path including the filename and extension 
file_path <- file.path(folder_path, "working_data_final_sr2.csv")

# Export the dataframe to CSV 
write.csv(persuasion_merged, file = file_path, row.names = FALSE)

# Check if file exists 
file.exists(file_path) 
